// -----------------------------------------------------------------------
//  <copyright file="ExpressionExtensions.cs" company="LiuliuSoft">
//      Copyright (c) 2022-2024 DaprPlus. All rights reserved.
//  </copyright>
//  <site>https://www.dapr.plus</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2024-04-18 13:04</last-date>
// -----------------------------------------------------------------------

namespace DaprPlus.Extensions;

public static class ExpressionExtensions
{
    /// <summary>
    /// 查询表达式反转
    /// </summary>
    public static Expression<Func<T, bool>> Not<T>(this Expression<Func<T, bool>> expression)
    {
        var body = Expression.Not(expression.Body);
        return Expression.Lambda<Func<T, bool>>(body, expression.Parameters);
    }

    /// <summary>
    /// 标记异常的日志已经被记录过了
    /// </summary>
    /// <param name="ex">异常信息</param>
    public static void MarkLogged(this Exception ex)
    {
        ex.Data["Logged"] = true;
    }

    /// <summary>
    /// 获取异常日志是否已被记录
    /// </summary>
    public static bool IsLogged(this Exception ex)
    {
        return ex.Data.Contains("Logged") && ex.Data["Logged"] is true;
    }
}
